<template xmlns="http://www.w3.org/1999/html">
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="90px">
      <el-form-item label="评价" prop="grade">
        <el-select
          v-model="queryParams.grade"
          placeholder="请选择评价"
          clearable
          style="width: 240px"
        >
          <el-option
            v-for="dict in dict.type.mall_goods_evaluation_grade"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="页面展示" prop="state">
        <el-select
          v-model="queryParams.isShow"
          placeholder="页面展示"
          clearable
          style="width: 240px"
        >
          <el-option
            v-for="dict in dict.type.sys_is_enable"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="评价时间">
        <el-date-picker
          v-model="dateRange"
          style="width: 240px"
          value-format="yyyy-MM-dd"
          type="daterange"
          range-separator="-"
          start-placeholder="开始日期"
          end-placeholder="结束日期"
        ></el-date-picker>
      </el-form-item>







      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="dataList" >
      <el-table-column label="商品名称" align="center" prop="goodsName" />
      <el-table-column label="会员名称" align="center" prop="memberName" />
      <el-table-column label="评价" align="center" prop="grade">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.mall_goods_evaluation_grade" :value="scope.row.grade"/>
        </template>
      </el-table-column>

      <el-table-column label="物流评分" align="center" prop="deliveryScore" />
      <el-table-column label="服务评分" align="center" prop="serviceScore" />
      <el-table-column label="描述评分" align="center" prop="descriptionScore" />

      <el-table-column label="评价时间" align="center" prop="createTime" />
      <el-table-column label="页面展示" align="center" prop="promotionStatus" >
        <template slot-scope="scope">
          <dict-tag :options="dict.type.sys_is_enable" :value="scope.row.isShow"/>
        </template>

      </el-table-column>
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">

        <template slot-scope="scope">
          <el-dropdown trigger="click">
            <span class="el-dropdown-link">
              更多<i class="el-icon-arrow-down el-icon--right"></i>
            </span>
            <el-dropdown-menu slot="dropdown" >
              <el-dropdown-item v-hasPermi="['mall-manager:coupon:query']">
                <el-button
                  size="mini"
                  type="text"
                  icon="el-icon-viewt"
                  @click="handleView(scope.row)"
                >查看</el-button>
              </el-dropdown-item>
              <el-dropdown-item v-if="scope.row.replyStatus == 0" v-hasPermi="['mall-manager:evaluation:reply']">
                <el-button
                  size="mini"
                  type="text"
                  icon="el-icon-edit"
                  @click="handleReply(scope.row)"
                >回复</el-button>
              </el-dropdown-item>
              <el-dropdown-item v-if="scope.row.isShow == 0" v-hasPermi="['mall-manager:evaluation:edit']">
                <el-button
                  size="mini"
                  type="text"
                  icon="el-icon-check"
                  @click="handleChgIsShow(scope.row,1)"
                >展示</el-button>
              </el-dropdown-item>
              <el-dropdown-item v-if="scope.row.isShow == 1" v-hasPermi="['mall-manager:evaluation:edit']">
                <el-button
                  size="mini"
                  type="text"
                  icon="el-icon-close"
                  @click="handleChgIsShow(scope.row,0)"
                >隐藏</el-button>
              </el-dropdown-item>
              <el-dropdown-item command="a"    v-hasPermi="['mall-manager:evaluation:remove']" >
                <el-button
                  size="mini"
                  type="text"
                  icon="el-icon-delete"
                  @click="handleDelete(scope.row)"
                >删除</el-button>
              </el-dropdown-item>
            </el-dropdown-menu>
          </el-dropdown>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!--  查看   -->
    <el-dialog title="查看" :visible.sync="viewopen" v-if="viewopen" width="70%" append-to-body>
      <evaluation-view :evaluationId="id"></evaluation-view>
      <div slot="footer" class="dialog-footer">
        <el-button @click="viewopen = false">取 消</el-button>
      </div>
    </el-dialog>

    <!--  回复   -->
    <el-dialog title="回复" :visible.sync="replyopen" v-if="replyopen" width="70%" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="回复内容" prop="reply">
          <el-input
            type="textarea"
            :autosize="{ minRows: 2, maxRows: 4}"
            placeholder="请输入回复内容"
            v-model="form.reply"
            maxlength="400"
            show-word-limit
          >
          </el-input>
        </el-form-item>
        <el-form-item label="图片" prop="imgs">
          <el-upload
            :limit="imgLimit"
            :file-list="imgList"
            :action="imgction"
            :headers="headers"
            list-type="picture-card"
            :on-preview="handlePictureCardPreview"
            :on-success="handleSuccess"
          >
            <i class="el-icon-plus"></i>
          </el-upload>
        </el-form-item>

      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="replyopen = false">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import {listEvaluation, replyEvaluation, deleteEvaluation, chgIsShow} from "@/api/mall/goods/evaluation";
import EvaluationView from './view'
import {mapGetters} from "vuex";
export default {
  name: "GoodsEvaluation",
  dicts: ['sys_is_enable','mall_goods_evaluation_grade'],
  components: {EvaluationView},
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 表格数据
      dataList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      replyopen: false,
      viewopen: false,
      // 日期范围
      dateRange: [],
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        grade: null,
        isShow: null,
      },
      // 表单参数
      form: {},
      id: null,
      imgLimit: 3,
      imgList: [],
      imgction:process.env.VUE_APP_BASE_API + '/common/oss/upload',
      // 表单校验
      rules: {
        reply: [
          { required: true, message: "回复内容不能为空", trigger: "blur" },
          { min: 1, max: 400, message: '内容长度必须介于 1 和 400 之间', trigger: 'blur' }
        ],
      },
    };
  },
  computed: {
    ...mapGetters(['token']),
    headers() {
      return{
        "Authorization": this.token // 直接从本地获取token就行
      }
    }
  },
  created() {
    this.getList();
  },
  methods: {
    /** 查询商城优惠券列表 */
    getList() {
      this.loading = true;
      if (this.dateRange.length > 0){
        this.queryParams.startTime = this.dateRange[0]
        this.queryParams.endTime = this.dateRange[1]
      }
      listEvaluation(this.queryParams).then(response => {
        this.dataList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        reply: '',
        replyImage: ''
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },

    /** 查看按钮操作 */
    handleView(row) {
      this.viewopen = true;
      this.id = row.id
    },

    /** 回复按钮操作 */
    handleReply(row) {
      this.replyopen = true;
      this.id = row.id
      this.reset()
      this.form.id = row.id
    },
    /** 展示/隐藏按钮操作 */
    handleChgIsShow(row,isShow) {
      let form = {
        id: row.id,
        isShow: isShow
      }

      chgIsShow(form).then(response => {
        this.$modal.msgSuccess("修改成功");
        this.getList();
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          this.form.replyImage = this.imgList.join(",")
          replyEvaluation(this.form).then(response => {
            this.$modal.msgSuccess("评价成功");
            this.replyopen = false;
            this.getList();
          });
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$modal.confirm('是否确认删除？').then(function() {
        return deleteEvaluation(ids);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {});
    },


    handleSuccess(res,file, fileList) {
      console.log('res: ' + JSON.stringify(res))
      console.log('fileList: ' + JSON.stringify(fileList));
      this.imgList.push( res.data.url);
    },
    handlePictureCardPreview(file) {
      this.dialogImageUrl = file.url;
      this.dialogVisible = true;
    },
  }
};
</script>
